// detail-medical.ts - 医护端任务详情页
interface TaskData {
  id: string
  type: string
  typeIcon: string
  typeName: string
  startLocation: string
  startAddress?: string
  endLocation: string
  endAddress?: string
  status: string
  statusText: string
  urgencyLevel: string
  urgencyText: string
  passengerCount: number
  createTime: string
  contactPhone: string
  distance: number
  duration: number
}

interface PatientInfo {
  description: string
  images: string[]
  videos: string[]
}

Page({
  data: {
    taskId: '',
    statusBarHeight: '44px',
    taskData: null as TaskData | null,
    patientInfo: null as PatientInfo | null,
    showMap: false,
    currentLocation: null as any,
    mapMarkers: [] as any[],
    remarks: ''
  },

  onLoad(options: any) {
    this.setStatusBarHeight()
    this.checkLogin()
    
    const { id } = options
    if (id) {
      this.setData({ taskId: id })
      this.loadTaskDetail()
    }
  },

  setStatusBarHeight() {
    const windowInfo = (wx as any).getWindowInfo()
    const statusBarHeight = windowInfo.statusBarHeight || 44
    this.setData({
      statusBarHeight: statusBarHeight + 'px'
    })
  },

  checkLogin() {
    const userRole = wx.getStorageSync('userRole')
    if (userRole !== 'medical') {
      wx.reLaunch({ url: '/pages/login/login' })
    }
  },

  goBack() {
    wx.navigateBack()
  },

  loadTaskDetail() {
    wx.showLoading({ title: '加载中...' })
    
    setTimeout(() => {
      const mockData: TaskData = {
        id: this.data.taskId,
        type: 'ambulance',
        typeIcon: '🚑',
        typeName: '救护车',
        startLocation: '辽宁省-沈阳市',
        startAddress: '某某东大街112号院东恒生宇楼B座11.5',
        endLocation: '北京市-东城区',
        endAddress: '协和医院本部',
        status: 'in_progress',
        statusText: '已发车',
        urgencyLevel: 'low',
        urgencyText: '低',
        passengerCount: 2,
        createTime: '2025-09-07 周日 09:22',
        contactPhone: '135 0000 0000',
        distance: 680,
        duration: 450
      }
      
      const patientInfo: PatientInfo = {
        description: '患者男性，65岁，突发胸痛伴呼吸困难约2小时，既往有高血压病史10年，规律服药控制。目前意识清楚，面色苍白，出汗较多。',
        images: [
          'https://picsum.photos/400/400?random=1',
          'https://picsum.photos/400/400?random=2'
        ],
        videos: []
      }
      
      this.setData({
        taskData: mockData,
        patientInfo: patientInfo,
        showMap: mockData.status === 'in_progress',
        currentLocation: {
          latitude: 39.908823,
          longitude: 116.397470
        }
      })
      
      this.updateMapMarkers()
      wx.hideLoading()
    }, 800)
  },

  updateMapMarkers() {
    if (!this.data.currentLocation) return
    
    const markers = [{
      id: 1,
      latitude: this.data.currentLocation.latitude,
      longitude: this.data.currentLocation.longitude,
      width: 40,
      height: 40,
      label: {
        content: '🚑',
        fontSize: 24,
        color: '#FF5252',
        bgColor: '#ffffff',
        borderRadius: 20,
        padding: 5
      }
    }]
    
    this.setData({ mapMarkers: markers })
  },

  uploadImage() {
    wx.chooseImage({
      count: 9,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: () => {
        wx.showLoading({ title: '上传中...' })
        setTimeout(() => {
          wx.hideLoading()
          wx.showToast({ title: '上传成功', icon: 'success' })
        }, 1500)
      }
    })
  },

  uploadVideo() {
    wx.chooseVideo({
      sourceType: ['album', 'camera'],
      maxDuration: 60,
      camera: 'back',
      success: () => {
        wx.showLoading({ title: '上传中...' })
        setTimeout(() => {
          wx.hideLoading()
          wx.showToast({ title: '上传成功', icon: 'success' })
        }, 1500)
      }
    })
  },

  previewPatientImage(e: any) {
    const index = e.currentTarget.dataset.index
    const patientInfo = this.data.patientInfo
    const images = patientInfo && patientInfo.images ? patientInfo.images : []
    wx.previewImage({
      current: images[index],
      urls: images
    })
  },

  onRemarksBlur(e: any) {
    const value = e.detail.value
    this.setData({ remarks: value })
  },

  confirmArrival() {
    wx.showModal({
      title: '确认到达',
      content: '确认已到达目的地？',
      success: (res) => {
        if (res.confirm) {
          wx.showToast({
            title: '已确认到达',
            icon: 'success'
          })
          setTimeout(() => wx.navigateBack(), 1500)
        }
      }
    })
  },

  showMore() {
    wx.showActionSheet({
      itemList: ['联系调度中心', '导出详情', '分享任务'],
      success: (res) => {
        switch(res.tapIndex) {
          case 0:
            wx.makePhoneCall({ phoneNumber: '400-123-4567' })
            break
          case 1:
          case 2:
            wx.showToast({ title: '功能开发中', icon: 'none' })
            break
        }
      }
    })
  }
})

